package dom.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dom.util.Connections;

public class DoDao extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public DoDao() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
    	request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        
        response.setContentType("text/html;charset=utf-8");
		
		
		com.mysql.jdbc.Connection con=Connections.getConnection();
        PreparedStatement  pstmt=null;
        ResultSet rs=null;
		String type=request.getParameter("type");
		String sql1="insert into productinfo (id,classid,title,price,specifications,place,alcohol,photo,content) values(?,?,?,?,?,?,?,?,?)";
		String sql2="delete from productinfo where id=? and classid=?";
		String sql2_1="delete from redwineinfo where id=?";
		String sql2_2="delete from companynews where id=?";
		String sql3="delete from record where studentid=? and domi_Build=? and domi_Code=? and id=?";
		String sql4="Update productinfo set title=?,price=?,specifications=?,place=?,alcohol=?,photo=?,content=? where id=? and classid=?";
		String sql4_1="insert into redwineinfo (id,title,author,content,date) values(?,?,?,?,?)";
		String sql4_2="insert into productcat (id,title) values(?,?)";
		String sql4_3="insert into companynews (id,title,author,content,date) values(?,?,?,?,?)";

		String sql5="Update redwineinfo set title=?,author=?,content=?,date=? where id=?";
		String sql5_1="Update productcat set title=? where id=?";
		String sql5_2="Update companynews set title=?,author=?,content=?,date=? where id=?";
		String sql8="Update manager set password=? where id=?";
		String sql9="Update manager set name=? where id=?";

		if(type.equals("ProductAdd")){
			int id=Integer.parseInt(request.getParameter("id")) ;
			int catid=Integer.parseInt(request.getParameter("classid")) ;
			String title=request.getParameter("title");
			String price=request.getParameter("price");
			String scale=request.getParameter("scale");
			String place=request.getParameter("place");
			String alcohol=request.getParameter("alcohol");
			String pic=request.getParameter("pic");
			String info=request.getParameter("info");
	        try {
				pstmt= con.prepareStatement(sql1);
				pstmt.setInt(1, id);
				pstmt.setInt(2, catid);
				pstmt.setString(3, title);
				pstmt.setString(4, price);
				pstmt.setString(5, scale);
				pstmt.setString(6, place);
				pstmt.setString(7, alcohol);
				pstmt.setString(8, pic);
				pstmt.setString(9, info);
		        pstmt.execute();
		        request.setAttribute("mainRight", "Result.jsp?result="+"产品添加成功");
				request.getRequestDispatcher("../Main.jsp").forward(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 

		}else if(type.equals("KnoAdd")){
			int id=Integer.parseInt(request.getParameter("id")) ;
			String title=request.getParameter("title");
			String author=request.getParameter("author");
			String content=request.getParameter("content");
			String date=request.getParameter("date");
	        try {
				pstmt= con.prepareStatement(sql4_1);
				pstmt.setString(2, title);
				pstmt.setString(3, author);
				pstmt.setString(4, content);
				pstmt.setString(5, date);
				pstmt.setInt(1, id);
		        pstmt.execute();
		        request.setAttribute("mainRight", "Result.jsp?result="+"红酒知识添加成功");
				request.getRequestDispatcher("../Main.jsp").forward(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(type.equals("WineCatAdd")){
			int id=Integer.parseInt(request.getParameter("id")) ;
			String title=request.getParameter("title");
	        try {
				pstmt= con.prepareStatement(sql4_2);
				pstmt.setString(2, title);
				pstmt.setInt(1, id);
		        pstmt.execute();
		        request.setAttribute("mainRight", "Result.jsp?result="+"分类信息添加成功");
				request.getRequestDispatcher("../Main.jsp").forward(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(type.equals("NewsAdd")){
			int id=Integer.parseInt(request.getParameter("id")) ;
			String title=request.getParameter("title");
			String author=request.getParameter("author");
			String content=request.getParameter("content");
			String date=request.getParameter("date");
	        try {
				pstmt= con.prepareStatement(sql4_3);
				pstmt.setString(2, title);
				pstmt.setString(3, author);
				pstmt.setString(4, content);
				pstmt.setString(5, date);
				pstmt.setInt(1, id);
		        pstmt.execute();
		        request.setAttribute("mainRight", "Result.jsp?result="+"资讯添加成功");
				request.getRequestDispatcher("../Main.jsp").forward(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(type.equals("ProductDel")){
			int id=Integer.parseInt(request.getParameter("id")) ;
			int catid=Integer.parseInt(request.getParameter("catid")) ;
	        try {
				pstmt= con.prepareStatement(sql2);
				pstmt.setInt(1, id);
				pstmt.setInt(2, catid);
		        pstmt.execute();
		        request.setAttribute("mainRight", "Result.jsp?result="+"产品删除成功");
				request.getRequestDispatcher("../Main.jsp").forward(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}else if(type.equals("KnoDel")){
			int id=Integer.parseInt(request.getParameter("id")) ;
	        try {
				pstmt= con.prepareStatement(sql2_1);
				pstmt.setInt(1, id);
		        pstmt.execute();
		        request.setAttribute("mainRight", "Result.jsp?result="+"红酒知识删除成功");
				request.getRequestDispatcher("../Main.jsp").forward(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}else if(type.equals("NewsDel")){
			int id=Integer.parseInt(request.getParameter("id")) ;
	        try {
				pstmt= con.prepareStatement(sql2_2);
				pstmt.setInt(1, id);
		        pstmt.execute();
		        request.setAttribute("mainRight", "Result.jsp?result="+"新闻删除成功");
				request.getRequestDispatcher("../Main.jsp").forward(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}else if(type.equals("KnoUp")){
			int id=Integer.parseInt(request.getParameter("id")) ;
			String title=request.getParameter("title");
			String author=request.getParameter("author");
			String content=request.getParameter("content");
			String date=request.getParameter("date");
	        try {
				pstmt= con.prepareStatement(sql5);
				pstmt.setString(1, title);
				pstmt.setString(2, author);
				pstmt.setString(3, content);
				pstmt.setString(4, date);
				pstmt.setInt(5, id);
		        pstmt.execute();
		        request.setAttribute("mainRight", "Result.jsp?result="+"产品信息修改成功");
				request.getRequestDispatcher("../Main.jsp").forward(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(type.equals("NewsUp")){
			int id=Integer.parseInt(request.getParameter("id")) ;
			String title=request.getParameter("title");
			String author=request.getParameter("author");
			String content=request.getParameter("content");
			String date=request.getParameter("date");
	        try {
				pstmt= con.prepareStatement(sql5_2);
				pstmt.setString(1, title);
				pstmt.setString(2, author);
				pstmt.setString(3, content);
				pstmt.setString(4, date);
				pstmt.setInt(5, id);
		        pstmt.execute();
		        request.setAttribute("mainRight", "Result.jsp?result="+"公司资讯修改成功");
				request.getRequestDispatcher("../Main.jsp").forward(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(type.equals("WineCatUp")){
			int id=Integer.parseInt(request.getParameter("id")) ;
			String title=request.getParameter("title");
	        try {
				pstmt= con.prepareStatement(sql5_1);
				pstmt.setString(1, title);
				pstmt.setInt(2, id);
		        pstmt.execute();
		        request.setAttribute("mainRight", "Result.jsp?result="+"分类信息修改成功");
				request.getRequestDispatcher("../Main.jsp").forward(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(type.equals("ProductUpdate")){
			int id=Integer.parseInt(request.getParameter("id")) ;
			int catid=Integer.parseInt(request.getParameter("catid")) ;
			String title=request.getParameter("title");
			String price=request.getParameter("price");
			String scale=request.getParameter("scale");
			String place=request.getParameter("place");
			String alcohol=request.getParameter("alcohol");
			String photo=request.getParameter("photo");
			String content=request.getParameter("content");
	        try {
				pstmt= con.prepareStatement(sql4);
				pstmt.setInt(8, id);
				pstmt.setInt(9, catid);
				pstmt.setString(1, title);
				pstmt.setString(2, price);
				pstmt.setString(3, scale);
				pstmt.setString(4, place);
				pstmt.setString(5, alcohol);
				pstmt.setString(6, photo);
				pstmt.setString(7, content);
		        pstmt.execute();
		        request.setAttribute("mainRight", "Result.jsp?result="+"产品信息修改成功");
				request.getRequestDispatcher("../Main.jsp").forward(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(type.equals("PSW")){
			String count=request.getParameter("count");
			String psw=request.getParameter("psw1");
			try {
				pstmt= con.prepareStatement(sql8);
				pstmt.setString(1, psw);
				pstmt.setInt(2, Integer.parseInt(count));
		        pstmt.execute();
		        request.setAttribute("mainRight", "Result.jsp?result="+"密码修改成功");
				request.getRequestDispatcher("../Main.jsp").forward(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(type.equals("Info")){
			String count=request.getParameter("count");
			String name=request.getParameter("name");
			try {
				pstmt= con.prepareStatement(sql9);
				pstmt.setInt(2, Integer.parseInt(count) );
				pstmt.setString(1, name);
		        pstmt.execute();
		        System.out.println(12312);
		        request.setAttribute("mainRight", "Result.jsp?result="+"个人信息修改成功");
				request.getRequestDispatcher("../Main.jsp").forward(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			
		}
		Connections.closeDB(con, pstmt, rs);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
